#!/usr/bin/env/python2.5

## Usage:  ~ input.trees 

## Strips a tree down to its ordered preterminal sequence.

import sys, re

def preterms(tree):
	"""Strips the string representation of a str(nltk.Tree) down to its ordered preterminal sequence."""
	pret = ""
	if (tree != "\n"):
		LCV = 0
		boughs = tree.split()
		while (LCV < len(boughs)):
			if (not re.match(r"\(", boughs[LCV + 1])):
				pret += boughs[LCV].lstrip("(") + " "
				LCV += 1
			LCV += 1
	return pret

if (__name__ == "__main__"):
	if (len(sys.argv) > 1):
		treefile = open(sys.argv[1], 'r')
		for forest in treefile:
			preterms(forest)
		treefile.close()
